/**
 * 价税分离规则 tab
 */
import React, { Component } from 'react';
import Condition from 'components/condition';
import priceTaxRuleService from './price-tax-seperation-service';
import { tableDecorator } from '../invoice-management/decorator';
import { messages } from 'share/common';
import 'styles/request-and-expense-settings/price-tax-rule.scss';
import SlideFrame from 'components/slide-frame';
import NewVatInvoice from './new-vat-invoice';
import InvoiceCheck from '../invoice-management/invoice-check';
import { Button } from 'antd';
import JumpConfigCenter from '../../setting/configuration-center/jump-config-center';
import Space from 'components/space';

const { If } = Condition;

@tableDecorator
class InvoiceControl extends Component {

  state = {
    config: {},
    columns: ['priority', 'companyNameList', 'operation'],
    searchColumns: ['companyNameList'],
  };

  componentDidMount() {
    this.handleInitData()
      .then();
  }

  handleInitData = async () => {
    const ret = await priceTaxRuleService.getDeductibleConfig();
    this.setState({ config: ret.data });
  };

  service = (page, pageSize, params) => {
    return priceTaxRuleService.getSeparationConfig({ page, pageSize, ...params });
  };

  handleSetConfig = () => {
    const { config } = this.state;
    SlideFrame.open({
      title: messages('common.create')/*新建*/,
      content: NewVatInvoice,
      afterClose: this.afterCloseSlideFrame,
      params: {
        ...config
      }
    }, this);
  };

  afterCloseSlideFrame = async (flag) => {
    flag && await this.handleInitData();
  };

  render() {
    const { config: { configFlag, receiptTypeList } } = this.state;
    return (
      <section className="price-tax-rule">
        <InvoiceCheck tab={'price-tax'} children={(
          <Space direction="vertical" size={"middle"}>
            <h3 className="price-tax-rule__h">{messages('expense-4.key30')/*可抵扣的发票*/}
              <If value={!configFlag}>
                <a className="price-tax-rule__a" onClick={this.handleSetConfig}>{messages('expense-4.key31')/*设置*/}</a>
              </If>
            </h3>
            {
              configFlag && <section className="price-tax-rule__p">
                {receiptTypeList && receiptTypeList.filter(item => item.enabled)
                  .map(item => item.receiptType)
                  .join('、')}
                <a className="price-tax-rule__a" onClick={this.handleSetConfig}>{messages('common.update')/*修改*/}</a>
              </section>
            }
            <JumpConfigCenter tab={'price-tax'}/>
            <h3 className="price-tax-rule__h2">{messages('expense-4.key32')/*费用价税分离规则*/}</h3>
          </Space>
        )}/>
      </section>
    );
  }
}

export default InvoiceControl;
